//
//  ViewController.m
//  HomeWork_10
//
//  Created by dllo on 16/3/22.
//  Copyright © 2016年 chen. All rights reserved.
//

#import "ViewController.h"
#import "TableView.h"
#import "SecondView.h"
#import "MyButton.h"
#import "AddViewController.h"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) NSArray *arrOfKey;
@property (nonatomic, retain) NSDictionary *dic;
@property (nonatomic, retain) UITableViewCell *cell;

@property (nonatomic, retain) AddViewController *add;

@end

@implementation ViewController

-(void)dealloc{
    [_arrOfKey release];
    [_dic release];
    [_cell release];
    [_add release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self createTableView];
    [self handleData];
    [self createBar];
    self.navigationItem.title = @"通讯录";
    self.view.backgroundColor = [UIColor whiteColor];

    
}
#pragma mark - 创建标题栏按钮
- (void)createBar{
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"jiahao"] style:UIBarButtonItemStylePlain target:self action:@selector(handleAdd:)];
    [self.navigationItem.rightBarButtonItem release];
}

//标题栏按钮的点击事件
- (void)handleAdd:(UIBarButtonItem *)bar{
    self.add = [[AddViewController alloc]init];
    [self.navigationController pushViewController:self.add animated:YES];
}

//调用信息
- (void)handleData{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"DLS160101" ofType:@"plist"];
    self.dic = [NSDictionary dictionaryWithContentsOfFile:path];
    self.arrOfKey = [[self.dic allKeys]sortedArrayUsingSelector:@selector(compare:)];
    
}
//创建tableView
- (void)createTableView{
    TableView *table = [[TableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    [self.view addSubview:table];
    [table release];
    table.dataSource = self;
    table.delegate = self;
    table.rowHeight = 58;
    table.separatorColor = [UIColor cyanColor];
    
}

#pragma mark - 实现UITableViewDataDelegate协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.arrOfKey.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSString *key = [self.arrOfKey objectAtIndex:section];
    NSArray *arr = [self.dic objectForKey:key];
    NSLog(@"%ld", arr.count);
    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath{
    static NSString *pool1 = @"pool";
    self.cell = [tableView dequeueReusableCellWithIdentifier:pool1];
    if (self.cell == nil) {
        self.cell = [[UITableViewCell alloc]initWithStyle:3 reuseIdentifier:pool1];
    }
    NSString *key = [self.arrOfKey objectAtIndex:indexPath.section];
    NSArray *arr = [self.dic objectForKey:key];
    NSDictionary *dic = [arr objectAtIndex:indexPath.row];
    self.cell.textLabel.text = [dic objectForKey:@"name"];
    return self.cell;
    
}

- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.arrOfKey;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [self.arrOfKey objectAtIndex:section];
}

#pragma mark - tableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    SecondView *second = [[SecondView alloc]init];
    [self.navigationController pushViewController:second animated:YES];
    NSString *key = [self.arrOfKey objectAtIndex:indexPath.section];
    NSArray *arr = [self.dic objectForKey:key];
    NSDictionary *dic = [arr objectAtIndex:indexPath.row];
    self.cell.textLabel.text = [dic objectForKey:@"name"];
    self.cell.detailTextLabel.text = [dic objectForKey:@"phoneNumber"];
    second.name = self.cell.textLabel.text;
    second.phoneNumber = self.cell.detailTextLabel.text;
    [second release];
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
